import { GCtrl } from "../../lib/util/GCtrl";
import DWC_DCContent from "./DWC_DCContent/DWC_DCContent";
import { DWC_DCContentData } from "./DWC_DCContent/DWC_DCContentItem/DWC_DCContentItem";
import DWC_DCEmoticon from "./DWC_DCEmoticon/DWC_DCEmoticon";
import DWC_DCMenu from "./DWC_DCMenu/DWC_DCMenu";

const { ccclass, property } = cc._decorator;
/** 复杂聊天界面 create by tm*/
@ccclass
export default class DWC_DetailChat extends cc.Component {
    //-----------组件区----------
    /** 聊天菜单区 */
    @property(cc.Node)
    node_DCMenu: cc.Node = null;
    @property(cc.Prefab)
    dcMenu_pfb: cc.Prefab = null;
    _dcMenu_ts: DWC_DCMenu = null;
    /** 聊天内容区 */
    @property(cc.Node)
    node_DCContent: cc.Node = null;
    @property(cc.Prefab)
    dcContent_pfb: cc.Prefab = null;
    _dcContent_ts: DWC_DCContent = null;
    /** 聊天输入区 */
    @property(cc.Node)
    node_DCInput: cc.Node = null;
    /** 聊天表情区 */
    @property(cc.Node)
    node_DCEmoticon: cc.Node = null;
    @property(cc.Prefab)
    dcEmoticon_pfb: cc.Prefab = null;
    //-----------变量区----------
    /** 外部事件 */
    outEvent: any = null;
    /** 聊天类型 1 私聊消息(玩家) 2 桌子消息(房间) 3 大厅消息 */
    chatType: number = null;
    /** 聊天id 
     * chatType == 1时,为接收者 rid
     * chatType == 2时,为接收者 桌子ID
     * chatType == 3时,为接收者 所属代理ID
    */
    chatId: any = null;
    onLoad() {
        this.initUI();
        this.addEvent();
    }
    /** 初始化界面 (先调用) */
    initUI(): void {
        //打开java输入框
        cc.vv.GLSDKMgr.openkeyboardJs();
    }
    /** 打开或关闭表情区 */
    openOrCloseEmoticon(): void {
        if (this.node_DCEmoticon.childrenCount > 0) {
            this.node_DCEmoticon.destroyAllChildren();
        } else {
            let _emoticon: DWC_DCEmoticon = this.createNode(this.dcEmoticon_pfb, this.node_DCEmoticon);
            _emoticon.init({
                emoticonParent: this.node
            });
        }
    }
    /** 初始化数据(外界调用) */
    init(_args): void {
        if (_args) {
            _args = _args[0];
            let _isPrivate: boolean = !!_args[`private`];
            //大厅与房间一定在私聊前
            this.addMenu(_args.isHall, _args.chatId, _isPrivate);
            this.addPrivateMenu(_args[`private`]);
        }
    }
    /** 添加事件 */
    addEvent(): void {
        this.node.on(`dwc_detailChat_touchMenu`, this.touchMenu, this);//点击 menu 
        this.node.on(`dwc_detailChat_touchEmoticon`, this.touchEmoticon, this);//点击 表情 
        //-------广播事件------
        this.outEvent = {};
        //收到服务器消息通知
        this.outEvent[`ServerNtc`] = cc.vv.eventCustom.on("ServerNtc", this.ServerNtc.bind(this), this.node);
        //发送玩家数据
        this.outEvent[`sendMsgToPlayer`] = cc.vv.eventCustom.on("sendMsgToPlayer", this.sendMsgToPlayer.bind(this), this.node);
        //表情
        this.outEvent[`openBq`] = cc.vv.eventCustom.on("openBq", this.openBq.bind(this), this.node);
        this.outEvent[`closeBq`] = cc.vv.eventCustom.on("closeBq", this.closeBq.bind(this), this.node);
        //玩家游戏列表变化
        this.outEvent[`PlayerEnter`] = cc.vv.eventCustom.on("PlayerEnter", this.PlayerEnter.bind(this), this.node);

    }
    /** 移除事件 */
    removeEvent(): void {
        this.node.off(`dwc_detailChat_touchMenu`, this.touchMenu, this);
        //-------广播事件------
        if (this.outEvent) {
            for (let _key in this.outEvent) {
                this.outEvent[_key] && cc.vv.eventCustom.off(`${_key}`, this.outEvent[_key]);
                delete this.outEvent[_key];
            }
        }
    }
    /**  */
    /** 添加节点 
     * @param _pfb 添加预制体
     * @param _root 添加节点
     * @param _pit 添加位置
    */
    createNode(_pfb: cc.Prefab, _root: cc.Node, _pit: cc.Vec2 = cc.v2(0, 0)): any {
        let _node: cc.Node = cc.instantiate(_pfb);
        if (!_node) {
            cc.log(`${_pfb.name}instantiate失败`);
            return;
        }
        _node.parent = _root;
        _pit && (_node.position = _pit);
        return _node.getComponent(_node.name);
    }
    /** 添加菜单 
     * @param _isPrivate 是不是点击私聊打开
    */
    addMenu(_isHall: boolean, _chatId: any, _isPrivate: boolean): void {
        //先添加聊天界面
        this._dcMenu_ts = this.createNode(this.dcMenu_pfb, this.node_DCMenu);
        this._dcMenu_ts.init({
            menuParent: this.node
        });
        this.chatType = 3;
        this.chatId = _chatId;
        //设置聊天menu
        let _menuArr: {
            menuName: string,
            isChoose: boolean,
            isDelete: boolean,
            type: number,
            chatId: any
        }[] = [{
            menuName: `公共`,
            isChoose: true,
            isDelete: false,
            type: this.chatType,
            chatId: _chatId
        }];
        if (!_isHall) {
            this.chatType = 2;
            _menuArr[0].isChoose = false;
            _menuArr[0].chatId = cc.vv.session.getParentRid();
            _menuArr.push({
                menuName: `房间`,
                isChoose: true,
                isDelete: false,
                type: this.chatType,
                chatId: _chatId
            });
        }
        if (_isPrivate) {
            _menuArr[_menuArr.length - 1].isChoose = false;
        } else {
            this.addChatContent(this.chatType, this.chatId);
        }
        _menuArr.forEach(_v => {
            this._dcMenu_ts.addMenuItem(_v).setRed(false);
        });
    }
    /** 添加私聊menu */
    addPrivateMenu(_private): void {
        if (!this._dcMenu_ts) return;
        //添加已存在menu
        let _pMenu: any[] = cc.vv.session.getAllPMenu();
        if (_pMenu && _pMenu.length > 0) {
            _pMenu.forEach(_v => {
                this._dcMenu_ts.addMenuItem(_v).setRed(false);
            });
        }
        if (!_private) return;
        //添加当前选中Meun
        this.chatType = 1;
        this.chatId = _private.chatId;
        //判断当前menu是否已存在
        let _isMenu: boolean = cc.vv.session.isPMenu(this.chatId, this.chatType);
        if (_isMenu) {
            this._dcMenu_ts.setChooseAndShowMenu(this.chatId, this.chatType);
        } else {
            let _menuItem = {
                menuName: _private.menuName,
                isChoose: false,
                isDelete: true,
                type: this.chatType,
                chatId: _private.chatId
            };
            cc.vv.session.setPMenu(_menuItem);
            let _item = this._dcMenu_ts.addMenuItem(_menuItem);
            _item.setRed(false);
            //设置选中
            this._dcMenu_ts.setChoose(_item.itemIdx, this.chatType, this.chatId);
            //显示最下方
            this._dcMenu_ts.showBtm();
        }
    }
    /** 设置聊天内容区 
     * @param _type 聊天类型 1 私聊消息(玩家) 2 桌子消息(房间) 3 大厅消息
     * @param _chatId: any//聊天id chatType == 1时,为接收者 rid; chatType == 2时,为接收者 桌子ID; chatType == 3时,为接收者 所属代理ID
    */
    addChatContent(_type: number, _chatId: any): void {
        this.node_DCEmoticon.destroyAllChildren();
        if (!this._dcContent_ts) {
            //添加聊天内容
            this._dcContent_ts = this.createNode(this.dcContent_pfb, this.node_DCContent);
            this._dcContent_ts.init({
                contentParent: this.node,
                chatType: _type,
                chatId: _chatId
            });
        } else {
            this._dcContent_ts.setChatTypeId(_type, _chatId);
        }
        //显示聊天内容
        this._dcContent_ts.addChatContent(this.getOneChatData(_chatId, _type), false, false);
    }
    //---------------------------内部事件区域---------------------------
    /** 点击menu */
    touchMenu(_evt): void {
        if (!_evt || !this._dcMenu_ts) return;
        this.chatId = _evt.chatId;
        this.chatType = _evt.type;
        //切换聊天内容
        this.addChatContent(this.chatType, this.chatId);
    }
    /** 点击发送表情 */
    touchEmoticon(_evt): void {
        if (!_evt || !this._dcContent_ts) return;
        this._dcContent_ts.playerSendMsg({
            content: _evt.emoticonName,
            chatType: 2//聊天类型 (1:文本;2:表情;3:语音;)
        });
    }
    //---------------------------外部事件区域---------------------------
    /** 广播事件 */
    ServerNtc(_evt): void {
        if (!_evt) return;
        _evt = _evt[0];
        switch (_evt.cmd) {
            case `ATTChatMessageNtc`://收到服务器聊天消息通知
                this.ATTChatMessageNtc(_evt);
                break;
        }
    }
    /** 收到服务器消息通知 */
    ATTChatMessageNtc(_evt): void {
        if (this._dcContent_ts && this._dcMenu_ts) {
            if (_evt.rid != cc.vv.session.getRid()) {
                /** 设置聊天 
                 * @param _isCurChat 是不是当前聊天消息
                */
                let _setChat = (_isCurChat: boolean) => {
                    let _content = cc.vv.session.getChatContent(_evt.content);
                    _isCurChat ? this._dcContent_ts.addChatContent([{
                        style: 1,
                        type: _content.contentType,
                        nick: _evt.rolename,
                        content: _content.contentTxt,
                        head: _evt.logo,
                        rid: _evt.rid,
                        itemY: 1,
                        height: -1,
                    }], true, false) : this._dcMenu_ts.setMenuItem({
                        menuName: _evt.rolename,
                        chatId: _evt.rid,
                        chatType: _evt.type
                    });
                }
                if (this._dcContent_ts.chatType != 1) {//玩家在大厅或房间
                    // true -> 收到大厅或房间消息; false -> 收到私聊信息
                    _setChat(_evt.type == this._dcContent_ts.chatType);
                } else {//玩家在私聊
                    if (_evt.type == this._dcContent_ts.chatType) {//收到私聊消息
                        // true -> 收到当前私聊玩家消息; false -> 收到其他玩家私聊信息
                        _setChat(_evt.rid == this._dcContent_ts.chatId);
                    } else {//收到大厅或房间消息
                        _setChat(false);
                    }
                }
            }
        }
    }
    /** 玩家发送消息 */
    sendMsgToPlayer(_evt): void {
        if (_evt && this._dcContent_ts) {
            _evt = _evt[0];
            this._dcContent_ts.playerSendMsg({
                content: _evt,
                chatType: 1,//聊天类型 (1:文本;2:表情;3:语音;)
            });
        }
    }
    /** 打开表情 */
    openBq(_evt): void {
        this.openOrCloseEmoticon();
    }
    /** 关闭表情 */
    closeBq(_evt): void {
        this.openOrCloseEmoticon();
    }
    /** 玩家游戏列表变化 */
    PlayerEnter(_evt): void {
        if (_evt && this._dcContent_ts) {
            _evt = _evt[0];
            this._dcContent_ts.addChatContent([{
                style: 3,
                type: 1,
                nick: _evt[`rolename`],
                content: `玩家${_evt[`rolename`]}进入游戏`,
                head: null,
                rid: null,
                itemY: 1,
                height: -1,
            }], true, true);
        }
    }
    /** 获得储存在本地某个会话的聊天信息 */
    getOneChatData(_chatId: any, _chatType: number): DWC_DCContentData[] {
        return cc.vv.session.getOneChatData(_chatId, _chatType);
    }
    //测试时发送消息
    tstSend(): void {
        if (this._dcContent_ts) {
            this._dcContent_ts.playerSendMsg({
                content: `成为广东省报废品不麻烦破不破旅客方面不发表看法颇尔`,
                chatType: 1,//聊天类型 (1:文本;2:表情;3:语音;)
            });
        }
    }
    /** 点击关闭 */
    touchClose(): void {
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
        GCtrl.UIMgr.close(GCtrl.GLConfig.DWC_DetailChat_path, GCtrl.UIMgr.closeType.destory);
    }
    onDestroy() {
        this.removeEvent();
        //移除房间信息
        cc.vv.session.removeOneChatData(null, 2);
        cc.vv.GLSDKMgr.closekeyboardJs();
    }
}
